package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysUserFriendBo;
import org.dromara.system.domain.vo.SysUserApplyVo;
import org.dromara.system.domain.vo.SysUserFriendCheckVo;
import org.dromara.system.domain.vo.SysUserFriendVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserFriendService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户好友信息
 *
 * @author Lion Li
 * @date 2025-02-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user/friend")
public class SysUserFriendController extends BaseController {

    private final ISysUserFriendService sysUserFriendService;

    /**
     * 查询用户好友信息列表(联系人列表)
     */
    @SaCheckPermission("system:friend:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserFriendVo> list(SysUserFriendBo bo, PageQuery pageQuery) {
        return sysUserFriendService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户好友信息列表
     */
    @SaCheckPermission("system:friend:export")
    @Log(title = "导出用户好友信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserFriendBo bo, HttpServletResponse response) {
        List<SysUserFriendVo> list = sysUserFriendService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户好友信息", SysUserFriendVo.class, response);
    }

    /**
     * 获取用户好友信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:friend:query")
    @GetMapping("/{id}")
    public R<SysUserFriendVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(sysUserFriendService.queryById(id));
    }

    /**
     * 新增用户好友信息
     */
    @SaCheckPermission("system:friend:add")
    @Log(title = "新增用户好友信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserFriendBo bo) {
        return toAjax(sysUserFriendService.insertByBo(bo));
    }

    /**
     * 修改用户好友信息
     */
    @SaCheckPermission("system:friend:edit")
    @Log(title = "修改用户好友信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserFriendBo bo) {
        return toAjax(sysUserFriendService.updateByBo(bo));
    }

    /**
     * 删除用户好友信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:friend:remove")
    @Log(title = "删除用户好友信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(sysUserFriendService.deleteWithValidById(id, true));
    }

    /**
     * 删除用户好友信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:friend:remove")
    @Log(title = "删除用户好友信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(sysUserFriendService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量判断是否自己好友
     *
     * @param uidList 用户id列表
     */
    @SaCheckPermission("system:friend:query")
    @Log(title = "批量判断是否自己好友")
    @GetMapping("/check")
    public R<SysUserFriendCheckVo> check(@NotEmpty(message = "用户好友id不能为空") @PathVariable List<Long> uidList) {
        return R.ok(sysUserFriendService.check(uidList));
    }

    /**
     * 申请好友
     *
     * @param msg       申请信息
     * @param targetUid 好友用户id
     */
    @SaCheckPermission("system:friend:add")
    @Log(title = "申请好友", businessType = BusinessType.INSERT)
    @GetMapping("/apply")
    public R<SysUserApplyVo> apply(@RequestParam("msg") String msg, @RequestParam("targetUid") Long targetUid) {
        return R.ok(sysUserFriendService.apply(msg, targetUid));
    }

    /**
     * 好友申请列表
     */
    @SaCheckPermission("system:friend:query")
    @Log(title = "好友申请列表")
    @GetMapping("/apply/list")
    public R<TableDataInfo<SysUserApplyVo>> applyList(PageQuery pageQuery) {
        return R.ok(sysUserFriendService.appplyList(pageQuery));
    }

    /**
     * 申请未读数量
     */
    @SaCheckPermission("system:friend:query")
    @Log(title = "申请未读数量")
    @GetMapping("/apply/unread")
    public R<Long> applyUnread() {
        return R.ok(sysUserFriendService.applyUnread());
    }

    /**
     * 同意好友申请
     *
     * @param id 申请id
     */
    @SaCheckPermission("system:friend:edit")
    @Log(title = "同意好友申请")
    @PutMapping("/apply/agree/{id}")
    public R<Void> agree(@PathVariable("id") Long id) {
        sysUserFriendService.agree(id);
        return R.ok("同意好友申请");
    }

    /**
     * 拒绝好友申请
     *
     * @param id 申请id
     */
    @SaCheckPermission("system:friend:edit")
    @Log(title = "拒绝好友申请")
    @PutMapping("/apply/reject/{id}")
    public R<Void> reject(@PathVariable("id") Long id) {
        return toAjax(sysUserFriendService.reject(id));
    }

    /**
     * 忽略审批
     *
     * @param id 申请id
     *
     */
    @SaCheckPermission("system:friend:edit")
    @Log(title = "忽略审批")
    @PutMapping("/apply/ignore/{id}")
    public R<Void> ignore(@PathVariable("id") Long id) {
        return toAjax(sysUserFriendService.ignore(id));
    }

    /**
     * 删除好友申请
     *
     * @param id 申请id
     */
    @SaCheckPermission("system:friend:remove")
    @Log(title = "删除好友申请")
    @DeleteMapping("/apply/{id}")
    public R<Void> deleteApply(@PathVariable("id") Long id) {
        return toAjax(sysUserFriendService.deleteApply(id));
    }

    /**
     * 修改好友备注
     *
     * @param targetUid 用户id
     * @param remark    备注
     */
    @SaCheckPermission("system:friend:update")
    @Log(title = "修改好友备注")
    @PutMapping("/remark")
    public R<Void> updateRemark(@RequestParam("targetUid") Long targetUid, @RequestParam("remark") String remark) {
        return toAjax(sysUserFriendService.updateRemark(targetUid, remark));
    }

    /**
     * 查找联系人
     *
     * @param phoneNumber 手机号
     * @param nickname    昵称
     */
    @SaCheckPermission("system:friend:query")
    @Log(title = "查找联系人")
    @GetMapping("/contacts")
    public R<List<SysUserVo>> findContacts(@RequestParam(name = "phoneNumber", required = false) String phoneNumber,
                                           @RequestParam(name = "nickname", required = false) String nickname) {
        if (ObjectUtil.isAllEmpty(phoneNumber, nickname)) {
            return R.fail("手机号和昵称不能同时为空");
        }
        return R.ok(sysUserFriendService.findContacts(phoneNumber, nickname));
    }

    /**
     * 好友权限设置
     */
    @SaCheckPermission("system:friend:edit")
    @Log(title = "好友权限设置")
    @PutMapping("/permission")
    public R<Void> updatePermission(@Validated(EditGroup.class) @RequestBody SysUserFriendBo bo) {
        return toAjax(sysUserFriendService.updatePermission(bo));
    }
}
